package com.per.hjh.datasource;

import java.sql.SQLFeatureNotSupportedException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;


public class DynamicDataSource extends AbstractRoutingDataSource {

	private Lock lock = new ReentrantLock(); 
	 private int counter = 0;  
	 @Value("${sourceNum}")
	 private Integer dataSourceNumber; 
	 private String [] v={"ds_admin","ds_partner"};


	@Override
	protected Object determineCurrentLookupKey() {

		lock.lock();  
        try{  
            counter++;  
            int lookupKey = counter %dataSourceNumber;  
    		System.err.println(lookupKey);
            return v[lookupKey];  
        }finally{  
            lock.unlock();  
        }  
	}

}
